package com.ruoyi.bizsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bizsys.domain.ExtTryQueue;
import com.ruoyi.bizsys.dto.PayTrans;
import com.ruoyi.bizsys.mapper.ExtTryQueueMapper;
import com.ruoyi.bizsys.service.IExtTryQueueService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.Func;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


/**
 * 接口重试Service业务层处理
 *
 * @author african-loan
 * @date 2022-03-04
 */
@Service
@Slf4j
public class ExtTryQueueServiceImpl extends ServiceImpl<ExtTryQueueMapper, ExtTryQueue> implements IExtTryQueueService {

    /**
     * 查询接口重试
     *
     * @param id 接口重试ID
     * @return 接口重试
     */
    @Override
    public ExtTryQueue selectExtTryQueueById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询接口重试列表
     *
     * @param extTryQueue 接口重试
     * @return 接口重试
     */
    @Override
    public List<ExtTryQueue> selectExtTryQueueList(ExtTryQueue extTryQueue) {
        return this.list(new QueryWrapper<>(extTryQueue).last("order by id desc"));
    }

    /**
     * 新增接口重试
     *
     * @param extTryQueue 接口重试
     * @return 结果
     */
    @Override
    public int insertExtTryQueue(ExtTryQueue extTryQueue) {
        extTryQueue.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(extTryQueue);
    }

    /**
     * 修改接口重试
     *
     * @param extTryQueue 接口重试
     * @return 结果
     */
    @Override
    public int updateExtTryQueue(ExtTryQueue extTryQueue) {
        extTryQueue.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(extTryQueue);
    }

    /**
     * 批量删除接口重试
     *
     * @param ids 需要删除的接口重试ID
     * @return 结果
     */
    @Override
    public int deleteExtTryQueueByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }


    @Override
    public void appendTransferQueue(PayTrans payTrans, String apiName) {

        QueryWrapper<ExtTryQueue> queryWrapper = new QueryWrapper<ExtTryQueue>().
                eq("serial_no", payTrans.getPayTransSerialNo()).eq("cust_no", payTrans.getCustNo());
        ExtTryQueue one = this.getOne(queryWrapper);
        if (Func.isEmpty(one)) {
            //新增
            ExtTryQueue extTryQueue = new ExtTryQueue();
            extTryQueue.setCustNo(payTrans.getCustNo());
            extTryQueue.setUserId(payTrans.getUserId());
            extTryQueue.setSerialNo(payTrans.getPayTransSerialNo());
            extTryQueue.setTryType(ExtTryQueue.TRANSFER);
            extTryQueue.setRemark(apiName);
            baseMapper.insert(extTryQueue);
            return;
        }
        plusOneById(one, apiName);
    }

    @Override
    public void appendQueue(String custNo, String tryType) {
        //serialNo 唯一字符串。
        String serialNo = tryType + "_" + custNo;
        ExtTryQueue extTryQueue = this.getOne(new QueryWrapper<ExtTryQueue>().eq("serial_no", serialNo));
        if (Func.isEmpty(extTryQueue)) {
            //新增
            extTryQueue = new ExtTryQueue();
            extTryQueue.setCustNo(custNo);
            extTryQueue.setSerialNo(serialNo);
            extTryQueue.setTryType(tryType);
            baseMapper.insert(extTryQueue);
            return;
        }
        //只有定时任务的操作才+1
        String name = Thread.currentThread().getName();
        log.info("currentThreadName:{}", name);
        if (Constants.CURRENT_NAME.equals(name)) {
            plusOneById(extTryQueue, "");
        }
    }

    @Override
    public void isExistPlusOne(String payTransSerialNo, String apiName) {
        ExtTryQueue extTryQueue = this.getOne(new QueryWrapper<ExtTryQueue>().eq("serial_no", payTransSerialNo));
        if (Func.isNotEmpty(extTryQueue)) {
            plusOneById(extTryQueue, apiName);
        }
    }

    private void plusOneById(ExtTryQueue extTryQueue, String apiName) {
        extTryQueue.setTryCount(extTryQueue.getTryCount() + 1);
        extTryQueue.setRemark(Func.isEmpty(extTryQueue.getRemark()) ? apiName : extTryQueue.getRemark() + "," + apiName);
        baseMapper.updateById(extTryQueue);
    }

    @Override
    public void transferSuccess(String payTransSerialNo, String custNo) {
        UpdateWrapper<ExtTryQueue> queryWrapper = new UpdateWrapper<ExtTryQueue>().
                eq("serial_no", payTransSerialNo).
                eq("cust_no", custNo).
                set("state", "1");
        this.update(queryWrapper);
    }

}
